package com.mz.mattress.client.service.impl;

import com.mz.mattress.api.aliyun.OssUtil;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.SysFile;
import com.mz.mattress.api.enums.FilePathEnum;
import com.mz.mattress.client.mapper.SysFileMapper;
import com.mz.mattress.client.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class SysFileServiceImpl implements SysFileService {

    @Autowired
    private SysFileMapper sysFileMapper;

    @Autowired
    private OssUtil ossUtil;

    @Override
    public Result<String> upload(MultipartFile multipartFile, FilePathEnum filePathEnum) {
        try {
            //  oss
            SysFile sysFile = ossUtil.uploadFile(multipartFile, filePathEnum);

            sysFileMapper.insert(sysFile);
            return Result.ok(sysFile.getUrl());
        } catch (Exception e) {
            log.error("upload_error", e);
            return Result.fail(e.getMessage());
        }
    }

}
